#include <stdlib.h>
#include <string.h>

#define __WORKINGNORMAL
#include "../../1_Application/Include/APP_WorkingNormalState.h"
#undef __WORKINGNORMAL

#include "../../2_Process/Include/Cmp_LQC_Process.h"
#include "../../2_Process/Include/Cmp_Onboarding.h"


#include "../../3_Component/Include/Cmp_KeyScan.h"
#include "../../3_Component/Include/Cmp_Display_Service.h"
#include "../../3_Component/Include/Cmp_SetError.h"


#include "../../4_Library/Include/Int.h"
#include "../../4_Library/Include/Lib_UartComm.h"
#include "../../4_Library/Include/Lib_EEPROM.h"
#include "../../4_Library/Include/Lib_Flash.h"


#include "../../5_WROS/Include/DDI_BootingDriver.h"
#include "../../5_WROS/Include/DDI_RegisterDriver.h"


#include "../../6_Vablesource/Include/Vable_ModelInformation.h"

void InitState(void);
void BPS_Choose(void);

UINT32* ReadingFlash(UINT32, UINT8);
void WriteingFlash(void);
void WriteingEEPROM(void);

extern const UINT8 gauCommInfo[PlatformMax][CommInformMax];

UINT8 EEPROM_MemoryInfo[8] = {0,};

void Run(UINT16 ucChechsum)
{
	InitState();

	while(1)
	{
		DDI_INIT_SetWDandNMIClear();
		KeyScan();
		Display_Service(ucChechsum);

		if(Get_JigKey() == CONFIRM_KEY)
		{
			Cmp_ErrorCheck();
			SetErrorCondition();

			BPS_Choose();

			Cmp_OnboardingCheck();
			Uart_Control();

			#ifndef	EEPROM_MEMORY
				WriteingFlash();
			#else
				WriteingEEPROM();
			#endif

			Run_LQC_ModeProcess();
		}
		else
		{;}
	}

}

void InitState()
{
	gunKey_New = CLR;

	PRE_ERROR = NO_ERROR;
	ERROR_State = NO_ERROR;
	ErrorCheckFlag = CLR;

	gunOpWriteFlag = CLR;
	gunCommOper = NormalComm;

	Standby_Signal_Flag = CLR;

	BPS_Choose_Flag = SET;
	Memory_Flag = CLR;
	EEPROM_Flag = CLR;

#ifndef	EEPROM_MEMORY
	ReadingFlash(0x58000, 8);
	Set_JigPlatform(MemorySpace[0]);//	Load from EEPROM
	Onboarding_ModelFlag = MemorySpace[4];
#else
        EEP_LoadBufferFromEEPROM(1,EEPROM_MemoryInfo,8);				//	Load from Flash
        Set_JigPlatform(EEPROM_MemoryInfo[0]);
        Onboarding_ModelFlag = EEPROM_MemoryInfo[1];
#endif

}

void BPS_Choose()
{
	if(BPS_Choose_Flag == SET)
	{
		BPS_Choose_Flag = CLR;
		switch(Get_JigPlatform())
		{
		    case Mega_Titan:
		    case JigUpdate:
		        DD_SetUARTWithDisplay(12,12);           //      9600
		        break;

		    case LCD_NT_VX:
		        DD_SetUARTWithDisplay(1,15);             //      115200
		        break;

		    default:
		        DD_SetUARTWithDisplay(6,14);            //      19200
		        break;
		}
	}
	else
	{;}
}

UINT32* ReadingFlash(UINT32 ulAddress, UINT8 ucSize)
{
	UINT8 ucTemp = 0;

	for(ucTemp = 0; ucTemp < ucSize; ucTemp++)
	{
		MemorySpace[ucTemp] = *(UINT16*)(ulAddress + 1);
	}

	return MemorySpace;
}

void WriteingFlash()
{
	T_CMD_STEP CMD_Step = CMD_ERASE_STEP;

	MemorySpace[0] = Get_JigPlatform();
	MemorySpace[1] = Onboarding_ModelFlag;

	SetCMDBuffer(MemorySpace);

	if(Memory_Flag == SET)
	{
		Memory_Flag = CLR;
		__disable_interrupt();
		while(CMD_Step != CMD_FINISH_STEP)
		{
			CMD_Step = mapfnFlashStepFunc[CMD_Step]();
		}
		__enable_interrupt();
	}
	else
	{;}
}


void WriteingEEPROM()
{
	if(EEPROM_Flag == SET)
	{
		EEPROM_Flag = CLR;
		EEPROM_MemoryInfo[0] = Get_JigPlatform();
		EEPROM_MemoryInfo[1] = Onboarding_ModelFlag;
		EEP_SaveBufferToEEPROM(1,EEPROM_MemoryInfo,8);
	}
	else
	{;}
}





